package com.sleep.psy.external_server.business.evaluation.controller;


import com.alibaba.fastjson.JSON;
import com.sleep.psy.external_server.business.evaluation.entity.EvaluationReport;
import com.sleep.psy.external_server.business.evaluation.entity.sleep.ReportReq;
import com.sleep.psy.external_server.business.evaluation.service.EvaluationReportService;
import com.sleep.psy.external_server.result.Result;
import com.sleep.psy.external_server.result.ResultEnum;
import com.sleep.psy.external_server.result.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 测评报告 前端控制器
 * </p>
 *
 * @author wenliguo
 * @since 2025-08-12
 */
@Api(tags = {"测评报告"})
@RestController
@RequestMapping("/evaluationReport")
public class EvaluationReportController {

    protected Logger logger = LoggerFactory.getLogger(EvaluationReportController.class);

    @Autowired
    private EvaluationReportService evaluationReportService;

    /**
     * 匹兹堡简易报告
     * @author wenliguo
     * @date 2025/6/6 10:41
     * @param req 请求参数
     * @return com.sleep.psy.main_server.result.Result<java.lang.Long>
     */
    @PostMapping("/simpleSleepReport")
    @ApiOperation(value = "获取匹兹堡简易报告")
    public Result<String> simpleSleepReport(@Valid @RequestBody ReportReq req) {

        logger.info("获取匹兹堡简易报告参数:{}", JSON.toJSONString(req));
        try {
            return evaluationReportService.simpleSleepReport(req.getReportId());
        } catch (Exception e) {
            logger.error("获取匹兹堡简易报告出错", e);
            return ResultUtil.error(ResultEnum.SERVER_INTERNAL_ERROR);
        }
    }

    /**
     * 获取匹兹堡专业报告
     * @author wenliguo
     * @date 2025/6/6 11:00
     * @param req 请求参数
     * @return com.sleep.psy.main_server.result.Result<java.lang.String>
     */
    @PostMapping("/professionalSleepReport")
    @ApiOperation(value = "获取匹兹堡专业报告")
    public Result<String> professionalSleepReport(@Valid @RequestBody ReportReq req) {

        logger.info("获取匹兹堡专业报告参数:{}", JSON.toJSONString(req));
        try {
            return evaluationReportService.professionalSleepReport(req.getReportId());
        } catch (Exception e) {
            logger.error("获取匹兹堡专业报告出错", e);
            return ResultUtil.error(ResultEnum.SERVER_INTERNAL_ERROR);
        }
    }



    /**
     * 获取睡眠问卷简易报告
     * @author wenliguo
     * @date 2025/6/6 15:29
     * @param req 请求参数
     * @return com.sleep.psy.main_server.result.Result<java.lang.String>
     */
    @PostMapping("/simpleSleepQuestionReport")
    @ApiOperation(value = "获取睡眠问卷简易报告")
    public Result<String> simpleSleepQuestionReport(@Valid @RequestBody ReportReq req) {

        logger.info("获取睡眠问卷简易报告参数:{}", JSON.toJSONString(req));
        try {
            return evaluationReportService.simpleSleepQuestionReport(req.getReportId());
        } catch (Exception e) {
            logger.error("获取睡眠问卷简易报告出错", e);
            return ResultUtil.error(ResultEnum.SERVER_INTERNAL_ERROR);
        }
    }

    /**
     * 获取失眠严重指数量表简易报告
     * @author wenliguo
     * @date 2025/7/10 09:09
     * @param req
     * @return com.sleep.psy.main_server.result.Result<java.lang.String>
     */
    @PostMapping("/simpleIsiReport")
    @ApiOperation(value = "获取失眠严重指数量表简易报告")
    public Result<String> simpleIsiReport(@Valid @RequestBody ReportReq req) {

        logger.info("获取失眠严重指数量表简易报告参数:{}", JSON.toJSONString(req));
        try {
            return evaluationReportService.simpleIsiReport(req.getReportId());
        } catch (Exception e) {
            logger.error("获取失眠严重指数量表简易报告出错", e);
            return ResultUtil.error(ResultEnum.SERVER_INTERNAL_ERROR);
        }
    }


    /**
     * 获取通用报告
     * @author wenliguo
     * @date 2025/7/23 14:30
     * @param req 请求
     * @return com.sleep.psy.main_server.result.Result<java.lang.String>
     */
    @PostMapping("/generalReport")
    @ApiOperation(value = "获取通用报告")
    public Result<String> generalReport(@Valid @RequestBody ReportReq req) {

        logger.info("获取通用报告参数:{}", JSON.toJSONString(req));
        try {
            return evaluationReportService.generalReport(req.getReportId());
        } catch (Exception e) {
            logger.error("获取通用报告出错", e);
            return ResultUtil.error(ResultEnum.SERVER_INTERNAL_ERROR);
        }
    }


    /**
     * 通过用户id获取全部有效报告
     * @author wenliguo
     * @date 2025/8/18 09:23
     * @param userId 用户id
     * @return com.sleep.psy.external_server.result.Result<java.util.List<com.sleep.psy.external_server.evaluation.entity.EvaluationReport>>
    */
    @GetMapping("/selectReportByUserId")
    @ApiOperation(value = "通过用户id获取全部有效报告")
    public Result<List<EvaluationReport>> selectReportByUserId(@RequestParam(value = "userId") Long userId) {
        try {
            return evaluationReportService.selectReportByUserId(userId);
        } catch (Exception e) {
            logger.error("通过用户id获取全部有效报告出错", e);
            return ResultUtil.error(ResultEnum.SERVER_INTERNAL_ERROR);
        }
    }

    /**
     * 通过报告id获取报告详情
     * @author wenliguo
     * @date 2025/8/18 09:45
     * @param reportId 报告id
     * @return com.sleep.psy.external_server.result.Result<com.sleep.psy.external_server.evaluation.entity.EvaluationReport>
    */
    @GetMapping("/selectReportById")
    @ApiOperation(value = "通过报告id获取报告详情")
    public Result<EvaluationReport> selectReportById(@RequestParam(value = "reportId") Long reportId) {
        try {
            return evaluationReportService.selectReportById(reportId);
        } catch (Exception e) {
            logger.error("通过报告id获取报告详情出错", e);
            return ResultUtil.error(ResultEnum.SERVER_INTERNAL_ERROR);
        }
    }

}

